package org.example.day09;

/**
 * 第三章节3.2.5.2: 方法join(long) 和 sleep(long)的区别
 *      ②join()方法会释放锁
 */
public class MyThread6 extends Thread{
    private Thread6B tb;

    public MyThread6(Thread6B tb) {
        this.tb = tb;
    }

    @Override
    public void run() {
        try {
            synchronized (tb){
                tb.start();
                tb.join(); //★★★ 让tb线程串行插队到ta线程中，同时ta线程释放锁tb ★★★
                //模拟长任务
                Thread.sleep(10000);
                System.out.println(System.currentTimeMillis());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Thread6B tb = new Thread6B();
            MyThread6 ta = new MyThread6(tb);
            ta.start();
            Thread.sleep(1000);
            Thread6C tc = new Thread6C(tb);
            tc.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread6B extends Thread{
    @Override
    public void run() {
        try {
            System.out.println("b run begin time = " + System.currentTimeMillis());
            Thread.sleep(5000);
            System.out.println("b run end time = " + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    synchronized public void bService(){
        System.out.println("b currentTime = " + System.currentTimeMillis());
    }
}

class Thread6C extends Thread{
    private Thread6B tb;

    public Thread6C(Thread6B tb) {
        this.tb = tb;
    }

    @Override
    public void run() {
        tb.bService();
    }
}